<?php
	defined('BASEPATH') OR exit('No direct script access allowed');
	class Admin_menu_mdl extends MY_Model{

		protected $_table = parent::T_M_MENU;
		/**
		 * 超级管理员角色ID
		 */
		const SUPER_ROLE_ID = 1;
		/**
		 * 栏目列表缓存文件key
		 */
		private $_cacheKey = 'admin_menu_list';
	
		public function __construct(){
			parent::__construct();
			$this->load->driver('cache', array('adapter' => 'file'));
		}
		/**
		 * 获取全部栏目里列表
		 */
		public function getAllMenuList(){
			$list = $this->cache->get($this->_cacheKey);
			if( !empty($list) ) return $list;
			$list = $this->db->select('*')
					->from($this->_table)
					->order_by('sort','ASC')
					->get()
					->result_array();
			$this->cache->save($this->_cacheKey,$list);
			return $list;
		}
		
		public function getMenuList($select='*',$where=''){
			if( $where ){
				$this->db->where($where);
			}
			return $this->db->select($select)->from($this->_table)->order_by('sort','ASC')->get()->result_array();
		}
		
		public function insert($data){
			$menuID = parent::insert($data);
			if( $menuID ){
				$this->db->set('rid',self::SUPER_ROLE_ID)
						->set('mid',$menuID)
						->insert(parent::T_M_ROLE_MENU);
				$this->deleteMenuCache();
			}
			return $menuID;
		}
		
		public function deleteByID($menuID){
			$where = array('id'=>$menuID);
			$res = parent::delete($where);
			if( $res ){
				$this->db->where('mid',$menuID)->delete(parent::T_M_ROLE_MENU);
				$this->deleteMenuCache();
			}
			return $res;
		}
		
		public function delete($where){
			$res = parent::delete($where);
			if( $res ){
				$this->deleteMenuCache();
			}
			return $res;
		}
		
		public function update($where, $data){
			$res = parent::update($where, $data);
			if($res){
				$this->cache->delete($this->_cacheKey);
			}
			return $res;
		}
		
		public function deleteMenuCache(){
			return $this->cache->delete($this->_cacheKey);
		}

		

		


	}